package com.zaofan.dao.base;

import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.SqlBuilderUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

public class DaoSupportImpl<T> implements DaoSupport<T> {
	private Class<T> clazz;
	private Logger logger = LoggerFactory.getLogger("dao");

	@SuppressWarnings("unchecked")
	public DaoSupportImpl() {
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazz = (Class<T>) pt.getActualTypeArguments()[0];
	}

	@Override
	public void add(T bean) {
		try {
			String sql = SqlBuilderUtils.builderInsertSql(clazz, bean);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void del(T bean) {
		try {
			String sql = SqlBuilderUtils.builderDelSql(clazz, bean);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(T bean) {
		try {
			String sql = SqlBuilderUtils.builderUpdateSql(clazz, bean);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<T> findAll(Map<String, Object> params) {
		try {
			String sql = SqlBuilderUtils.builderFindSql(clazz, params);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(clazz));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public T findOne(Map<String, Object> params) {
		try {
			String sql = SqlBuilderUtils.builderFindSql(clazz, params);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanHandler<>(clazz));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Long count(Map<String, Object> params) {
		try {
			String sql = SqlBuilderUtils.builderCountSql(clazz, params);
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	protected Connection getConnection() {
		return C3p0DataSourceUtils.getConnection();
	}

	protected DataSource getDataSource() {
		return C3p0DataSourceUtils.getDataSource();
	}

	protected void release(Connection conn, Statement st, ResultSet rs) {
		C3p0DataSourceUtils.release(conn, st, rs);
	}

	

}
